package com.credithc.customers.dataservice.Thread;
import com.credithc.eagleeye.plugins.thread.EagleEyeRunnable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

@Configuration
public class OldDataThreadPoolExecutor {
    private  ThreadPoolExecutor pool = null;
    @Value("${old.data.maxPoolSize}")
    private Integer maxPoolSize ;
    @Value("${old.data.corePoolSize}")
    private Integer corePoolSize ;



    /**
     * 线程池初始化方法
     *
     * corePoolSize 核心线程池大小----10
     * maximumPoolSize 最大线程池大小----30
     * keepAliveTime 线程池中超过corePoolSize数目的空闲线程最大存活时间----30+单位TimeUnit
     * TimeUnit keepAliveTime时间单位----TimeUnit.MINUTES
     * workQueue 阻塞队列----new ArrayBlockingQueue<Runnable>(10)====10容量的阻塞队列
     * threadFactory 新建线程工厂----new CustomThreadFactory()====定制的线程工厂
     * rejectedExecutionHandler 当提交任务数超过maxmumPoolSize+workQueue之和时,
     * 							即当提交第41个任务时(前面线程都没有执行完,此测试方法中用sleep(100)),
     * 						          任务会交给RejectedExecutionHandler来处理
     */
    public  void init() {
        if (maxPoolSize == null ||maxPoolSize<1){
            maxPoolSize = 5;
        }
        if (corePoolSize == null || corePoolSize <1){
            corePoolSize = 5;
        }
        pool = new ThreadPoolExecutor(
                5,
                maxPoolSize,
                30,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<Runnable>(10),
                new CustomThreadFactory(),
                new CustomRejectedExecutionHandler());
    }
    public  ExecutorService getCustomThreadPoolExecutor() {
        init();
        return this.pool;
    }

    private class CustomThreadFactory implements ThreadFactory {

        private AtomicInteger count = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(new EagleEyeRunnable(r));
            String threadName = OldDataThreadPoolExecutor.class.getSimpleName() + count.addAndGet(1);
            System.out.println(threadName);
            t.setName(threadName);
            return t;
        }
    }
    private class CustomRejectedExecutionHandler implements RejectedExecutionHandler {

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            // 记录异常
            // 报警处理等
            //System.out.println("error.............");
        }
    }
}
